﻿namespace BLL
{
    public class S_RiZhiXXBLL : S_RiZhiXXProcess
    {
        #region Field
        private string ConnectionString;
        #endregion
        #region Construction
        public S_RiZhiXXBLL()
        {
            ConnectionString = base.conn;
        }

//        /// <summary>
//        /// 项目基本信息 Entity
//        /// </summary>
//       // P_XiangMuJBXX _P_XiangMuJBXX = new P_XiangMuJBXX();

//        /// <summary>
//        /// 项目基本信息 BLL
//        /// </summary>
//       // P_XiangMuJBXXBLL _p_XiangMuJBXXBLL = new P_XiangMuJBXXBLL();

//        #endregion

//        #region 获取列注释
//        private static DataTable _ColComment = null;
//        private DataTable ColComment
//        {
//            get
//            {
//                if (null == _ColComment)
//                {
//                    string sql = @"select c.name as TableName,
//             (select  top 1 value from sys.extended_properties d where c.object_id=d.major_id and c.[type]='U' and d.minor_id=0) as TableDescription,
//             b.name as ColumnName,a.value as ColumuDescription from sys.extended_properties a 
//             inner join sys.columns b on a.major_id = b.object_id and a.minor_id = b.column_id
//             inner join sys.tables c on b.object_id = c.object_id and c.[type]='U'
//             order by c.name,b.column_id";
//                    _ColComment = SqlHelper.ExecuteDataset(Platform.Configuration.ConfigHelper.PlatformConnString, CommandType.Text, sql).Tables[0];
//                }
//                return _ColComment;
//            }
//        }

//        /// <summary>
//        /// 获取列注释,如果没有则返回列名
//        /// </summary>
//        /// <param name="tableName"></param>
//        /// <param name="columnName"></param>
//        /// <returns></returns>
//        public string ColComments(string tableName, string columnName)
//        {
//            DataRow[] dr = ColComment.Select("TableName='" + tableName + "' and ColumnName='" + columnName + "'");
//            string comment = columnName;
//            if (dr.Length > 0)
//            {
//                string[] _splitString = dr[0]["ColumuDescription"].ToString().Split('(');
//                if (_splitString.Length > 0)
//                    comment = _splitString[0].ToString();
//                else
//                    comment = dr[0]["ColumuDescription"].ToString();
//            }
//            return comment;
//        }

//        /// <summary>
//        /// 根据条件获取列
//        /// </summary>
//        /// <param name="filter"></param>
//        /// <returns></returns>
//        public DataTable ColComments(string filter)
//        {
//            DataRow[] dr = ColComment.Select(filter);
//            DataTable dt = ColComment.Clone();
//            dt.Rows.Clear();
//            for (int i = 0; i <= dr.Length - 1; i++)
//            {
//                dt.ImportRow((DataRow)dr[i]);
//            }
//            return dt;
//        }

//        #endregion

//        #region 获取表注释
//        private static DataTable _TableComment = null;

//        private DataTable TableComment
//        {
//            get
//            {
//                if (null == _TableComment)
//                {
//                    string sql = @"select c.name as TableName,
//        (select top 1 value from sys.extended_properties d where c.object_id=d.major_id and c.[type]='U' and d.minor_id=0) as TableDescription,
//        b.name as ColumnName,a.value as ColumuDescription from sys.extended_properties a 
//        inner join sys.columns b on a.major_id = b.object_id and a.minor_id = b.column_id
//        inner join sys.tables c on b.object_id = c.object_id and c.[type]='U'
//        order by c.name,b.column_id";
//                    _TableComment = SqlHelper.ExecuteDataset(Platform.Configuration.ConfigHelper.PlatformConnString, CommandType.Text, sql).Tables[0];
//                }
//                return _TableComment;
//            }
//        }

//        public string TableComments(string filter)
//        {
//            DataRow[] dr = TableComment.Select("TableName='" + filter + "'");
//            string comment = "";
//            if (dr.Length > 0)
//            {
//                comment = dr[0]["TableDescription"].ToString();
//            }
//            return comment;
//        }

//        /// <summary>
//        /// 根据条件获得所有的表和表注释
//        /// </summary>
//        /// <param name="sqlwhere"></param>
//        /// <returns></returns>
//        public DataTable TableCommentByCondition(string sqlwhere)
//        {
//            DataTable dt = new DataTable();

//            string sql = "select user_tab_comments.* from user_tab_comments,user_tables where TABLE_TYPE ='TABLE' and user_tab_comments.table_name = user_tables.TABLE_NAME AND user_tab_comments.Table_Name like '%" + sqlwhere + "%'";
//            dt = SqlHelper.ExecuteDataset(Platform.Configuration.ConfigHelper.PlatformConnString, CommandType.Text, sql).Tables[0];
//            return dt;
//        }



//        /// <summary>
//        /// 获取所有的表
//        /// </summary>
//        /// <returns></returns>
//        public DataTable GetAllTable()
//        {
//            return this.TableComment;
//        }

//        #endregion

//        #region 私有方法

//        /// <summary>
//        /// 改变转移字符
//        /// </summary>
//        /// <param name="value"></param>
//        /// <returns></returns>
//        private string ChangeValue(string value)
//        {
//            string result = value;
//            if (result.Contains("&"))
//            {
//                result = result.Replace("&", "&amp;");
//            }
//            if (result.Contains(">"))
//            {
//                result = result.Replace(">", "&gt;");
//            }
//            if (result.Contains("<"))
//            {
//                result = result.Replace("<", "&lt;");
//            }
//            if (result.Contains("\""))
//            {
//                result = result.Replace("\"", "&quot;");
//            }
//            if (result.Contains("'"))
//            {
//                result = result.Replace("'", "&apos;");
//            }
//            if (result.Contains("-"))
//            {
//                result = result.Replace("-", "\\-");
//            }
//            return result;
//        }

//        /// <summary>
//        /// 将NULL转字符串
//        /// </summary>
//        /// <param name="o"></param>
//        /// <returns></returns>
//        private static string NullToString(object o)
//        {
//            if (null == o)
//            {
//                return "null";
//            }
//            return o.ToString();
//        }

//        /// <summary>
//        /// 判断在实体中是否存在指定列
//        /// </summary>
//        /// <typeparam name="T"></typeparam>
//        /// <param name="entity"></param>
//        /// <param name="columnname"></param>
//        /// <returns></returns>
//        private bool IsExistColumnInEntity<T>(T entity, string columnname)
//        {
//            Type type = entity.GetType();
//            if (ColComments(type.Name, columnname).Trim() != columnname)
//                return true;
//            return false;
//        }

//        /// <summary>
//        /// 将单行DataTable转Entity
//        /// </summary>
//        /// <typeparam name="T"></typeparam>
//        /// <param name="dt"></param>
//        /// <param name="entity"></param>
//        /// <returns></returns>
//        private T ConvertDataTable<T>(DataTable dt, T entity)
//        {
//            Type type = entity.GetType();
//            DataRow dr = dt.Rows[0];
//            for (int i = 0; i < dt.Columns.Count; i++)
//            {
//                object o = dr[i];
//                string name = dt.Columns[i].ColumnName;
//                if (o != DBNull.Value)
//                {
//                    try
//                    {   //给属性赋值


//                        Type t = type.GetProperty(name).PropertyType;

//                        if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>))
//                            t = t.GetGenericArguments()[0];


//                        type.GetProperty(name).SetValue(entity, Convert.ChangeType(o, t), null);
//                    }
//                    catch //如果有错误,继续下一个属性的赋值   
//                    {
//                        continue;
//                    }
//                }
//            }
//            return entity;
//        }

//        /// <summary>
//        /// 将数据Log数据保存为XML文档形式
//        /// </summary>
//        /// <typeparam name="T"></typeparam>
//        /// <param name="opType"></param>
//        /// <param name="entity"></param>
//        /// <returns></returns>
//        private string CreateXmlLog<T>(Entity.OperateState opType, T entity, SqlTransaction tran)
//        {
//            string s = "";
//            if (opType == Entity.OperateState.Add)
//            {
//                Type type = entity.GetType();
//                string tableName = type.Name;
//                foreach (System.Reflection.PropertyInfo p in type.GetProperties())
//                {
//                    object o = p.GetValue(entity, null);
//                    string columnName = p.Name;
//                    string columnComment = ColComments(tableName, columnName);
//                    s += " <" + tableName + "." + columnName + " name='" + columnComment + "' oldvalue='' newvalue='" + ChangeValue(NullToString(o)) + "'/>" + System.Environment.NewLine;
//                }
//            }
//            else if (opType == Entity.OperateState.Modify)
//            {
//                Type type = entity.GetType();
//                string tableName = type.Name;

//                int id = 0;
//                try
//                {
//                    id = int.Parse(type.GetProperty("ID").GetValue(entity, null).ToString());
//                }
//                catch
//                {
//                    id = 0;
//                }

//                int sid = 0;
//                try
//                {
//                    sid = int.Parse(type.GetProperty("I_SID").GetValue(entity, null).ToString());
//                }
//                catch
//                {
//                    sid = 0;
//                }

//                BusinessTool businessTool = new BusinessTool();
//                DataTable dt = new DataTable();
//                if (id != 0)
//                    dt = businessTool.GetDataTable(type.Name, id, tran);
//                else
//                    dt = businessTool.GetDataTable(type.Name, sid, tran);

//                T entityOld = (T)Activator.CreateInstance(type);
//                entityOld = ConvertDataTable(dt, entityOld);
//                Type typeOld = entityOld.GetType();
//                int count = type.GetProperties().Length;
//                for (int i = 0; i < count; i++)
//                {
//                    object o = type.GetProperties()[i].GetValue(entity, null);
//                    object oOld = typeOld.GetProperties()[i].GetValue(entityOld, null);
//                    if (NullToString(oOld) != NullToString(o))
//                    {
//                        string columnName = type.GetProperties()[i].Name;
//                        string columnComment = ColComments(tableName, columnName);
//                        s += " <" + tableName + "." + columnName + " name='" + columnComment + "' oldvalue='" + ChangeValue(NullToString(oOld)) + "' newvalue='" + ChangeValue(NullToString(o)) + "'/>" + System.Environment.NewLine;
//                    }
//                }
//            }
//            return s;
//        }


//        /// <summary>
//        /// 根据表名和主键ID获取日志字符串
//        /// </summary>
//        /// <param name="opType"></param>
//        /// <param name="id"></param>
//        /// <param name="tableName"></param>
//        /// <returns></returns>
//        private string Log(Entity.OperateState opType, int id, string tableName, SqlTransaction tran)
//        {
//            string s = "";
//            if (opType == Entity.OperateState.Delete)
//            {
//                BusinessTool businessTool = new BusinessTool();
//                DataTable dt = businessTool.GetDataTable(tableName, id, tran);
//                for (int i = 0; i < dt.Columns.Count; i++)
//                {
//                    string columnComment = ColComments(tableName, dt.Columns[i].ColumnName);
//                    s += " <" + tableName + "." + dt.Columns[i].ColumnName + " name='" + columnComment + "' oldvalue='" + ChangeValue(NullToString(dt.Rows[0][i])) + "' newvalue=''/>" + System.Environment.NewLine;
//                }
//            }
//            return s;
//        }
//        #endregion

//        #region 公共方法

//        /// <summary>
//        /// 记录系统日志
//        /// </summary>
//        /// <typeparam name="T">泛型</typeparam>
//        /// <param name="account">用户登录账户</param>
//        /// <param name="modelName">模块名称</param>
//        /// <param name="buZhuBH">步骤编号</param>
//        /// <param name="opType">操作类型（新增，1，修改，2）</param>
//        /// <param name="entity">实体</param>
//        /// <param name="transaction">事务</param>
//        public bool Log<T>(Entity.LoginInfo loginInfo, string modelName, string buZhuBH, Entity.OperateState opType, T entity, SqlTransaction transaction)
//        {
//            BusinessTool businessTool = new BusinessTool();
//            Type type = entity.GetType();
//            Entity.S_RiZhiXX log = new Entity.S_RiZhiXX();
//            log.S_CaoZuoR = loginInfo.Account;
//            log.D_CaoZuoRQ = DateTime.Now;
//            log.S_BiaoMing = type.Name;
//            log.S_SuoShuMK = modelName;
//            log.S_BuZhouBH = buZhuBH;
//            log.S_IPDiZhi = loginInfo.IP;

//            try
//            {
//                log.S_XiangMuBH = IsExistColumnInEntity(entity, "S_XiangMuBH") == true ? type.GetProperty("S_XiangMuBH").GetValue(entity, null).ToString() : null;
//            }
//            catch
//            {
//                log.S_XiangMuBH = null;
//            }

//            try
//            {
//                log.I_SID = IsExistColumnInEntity(entity, "I_SID") == true ? Convert.ToInt32(type.GetProperty("I_SID").GetValue(entity, null)) : 0;
//                //if (log.S_XiangMuBH == null)
//                //{
//                //    _P_XiangMuJBXX = _p_XiangMuJBXXBLL.SelectP_XiangMuJBXX(log.I_SID.Value, transaction);
//                //    log.S_XiangMuBH = _P_XiangMuJBXX.S_XiangMuBH;
//                //}
//            }
//            catch
//            {
//                log.I_SID = null;
//            }

//            try
//            {
//                log.S_ZhuJian = type.GetProperty("ID").GetValue(entity, null).ToString();
//            }
//            catch
//            {
//                //log.S_ZhuJian = "";
//                log.S_ZhuJian = IsExistColumnInEntity(entity, "I_SID") == true ? Convert.ToInt32(type.GetProperty("I_SID").GetValue(entity, null)).ToString() : "";
//            }
//            //表示添加
//            if (opType == Entity.OperateState.Add)
//            {
//                log.S_CaoZuoLX = "添加";
//                StringBuilder logComment = new StringBuilder();
//                logComment.Append("<" + log.S_BiaoMing + " title='" + loginInfo.Account + "于" + DateTime.Now.ToString("yyyy-MM-dd HH:mm") + "向" + TableComments(type.Name) + "添加一条记录'>" + System.Environment.NewLine);
//                logComment.Append(CreateXmlLog(opType, entity, transaction));
//                logComment.Append("</" + log.S_BiaoMing + ">");
//                log.S_RiZhiNR = logComment.ToString();
//            }
//            else if (opType == Entity.OperateState.Modify)
//            {
//                log.S_CaoZuoLX = "修改";
//                StringBuilder logComment = new StringBuilder();
//                logComment.Append("<" + log.S_BiaoMing + " title='" + loginInfo.Account + "于" + DateTime.Now.ToString("yyyy-MM-dd HH:mm") + "向" + TableComments(type.Name) + "修改一条记录'>" + System.Environment.NewLine);
//                logComment.Append(CreateXmlLog(opType, entity, transaction));
//                logComment.Append("</" + log.S_BiaoMing + ">");
//                log.S_RiZhiNR = logComment.ToString();
//            }
//            else if (opType == Entity.OperateState.Delete)
//            {
//                log.S_CaoZuoLX = "删除";
//                StringBuilder logComment = new StringBuilder();
//                logComment.Append("<" + log.S_BiaoMing + " title='" + loginInfo.Account + "于" + DateTime.Now.ToString("yyyy-MM-dd HH:mm") + "向" + TableComments(type.Name) + "删除一条记录'>" + System.Environment.NewLine);
//                logComment.Append(Log(opType, int.Parse(log.S_ZhuJian), log.S_BiaoMing, transaction));
//                logComment.Append("</" + log.S_BiaoMing + ">");
//                log.S_RiZhiNR = logComment.ToString();
//            }

//            bool result = false;
//            if (transaction == null)
//                result = InsertOrUpdate(log, null);
//            else
//                result = InsertOrUpdate(log, transaction);

//            return result;
//        }

         #endregion




    }
}